package com.snail.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.snail.model.dto.UserTestSubmitDTO;
import com.snail.response.BaseResponse;
import com.snail.response.ResultUtils;
import com.snail.service.BizUserTestService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ysc
 * @descrpiton 前端用户做作业交互的接口
 * @date 2025/7/23 15:17
 **/
@RestController
@RequestMapping("/userTest")
@RequiredArgsConstructor
public class BizTestController {

    private final BizUserTestService bizUserTestService;

    @PostMapping("/submit")
    public BaseResponse<?> submitTest( @RequestBody UserTestSubmitDTO dto) {
        Integer userId= StpUtil.getLoginIdAsInt();
        boolean result = bizUserTestService.submitOrUpdateTest(userId, dto);
        return result ? ResultUtils.success("提交成功") : ResultUtils.error("提交失败");
    }

    @GetMapping("/list")
    public BaseResponse<?> list(@RequestParam Long userId) {
        return ResultUtils.success(bizUserTestService.listByUserId(userId));
    }

    @DeleteMapping("/{testId}")
    public BaseResponse<?> deleteTest(@PathVariable Long testId) {
        return bizUserTestService.deleteTestById(testId)
                ? ResultUtils.success("删除成功")
                : ResultUtils.error("删除失败");
    }

    @GetMapping("/detail/{testId}")
    public BaseResponse<?> detail(@PathVariable Long testId) {
        return ResultUtils.success(bizUserTestService.getTestDetail(testId));
    }

}
